package com.ganbo.stock.service.impl;

import com.ganbo.stock.config.DingDingConfig;
import com.ganbo.stock.config.Ig507Config;
import com.ganbo.stock.dto.SendGroupMessageReq;
import com.ganbo.stock.entity.FundStockLast;
import com.ganbo.stock.repository.FundStockLastHistoryRepository;
import com.ganbo.stock.repository.FundStockLastRepository;
import com.ganbo.stock.repository.FundStockRepository;
import com.ganbo.stock.service.FundService;
import com.ganbo.stock.service.FundStockLastHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 股票池
 *
 * @Author: ganbo
 * @Date: 2021/2/9 14:09
 */

@Service
public class FundStockLastHistoryServiceImpl extends BaseService implements FundStockLastHistoryService {

    @Autowired
    FundStockLastRepository fundStockLastRepository;
    @Autowired
    FundStockRepository fundStockRepository;
    @Autowired
    DingDingGroupMessageService dingDingGroupMessageService;
    @Autowired
    DingDingConfig dingDingConfig;
    @Autowired
    Ig507Config ig507Config;
    @Autowired
    FundService fundService;
    @Autowired
    FundStockLastHistoryRepository fundStockLastHistoryRepository;

    /**
     * 分析当日股票池数据
     */
    @Transactional
    public void analyseTodayStockPool() {

        Date now = new Date();

        //今日股票池数据
        List<FundStockLast> stockLasts = fundStockLastRepository.findFundStockLastByStatistics(now);
        if (CollectionUtils.isEmpty(stockLasts)) return;
        stockLasts.sort(((o1, o2) -> Math.toIntExact(o2.get持仓基金数量() - o1.get持仓基金数量()))); //按照持仓数量多少排序
        StringBuilder dingDingMessageContent = new StringBuilder("<==今日股票池==>\n 排|股票名字|持仓基金数量|日涨跌\n");
        for (int i = 0; i < stockLasts.size(); i++) {
            FundStockLast fundStockLast = stockLasts.get(i);
            dingDingMessageContent.append((i + 1) + " | " + fundStockLast.get名字()).append(" | "
                    + fundStockLast.get持仓基金数量() + " | " + fundStockLast.getPc() + "%").append("\n");
        }
        SendGroupMessageReq sendGroupMessageReq = new SendGroupMessageReq();
        sendGroupMessageReq.setCode("moreMoney");
        sendGroupMessageReq.setContent(dingDingMessageContent.toString());
        sendGroupMessageReq.setAtMobiles(dingDingConfig.getAtMobiles());
        dingDingGroupMessageService.sendGroupMessage(sendGroupMessageReq);

        //高抛低吸提醒
        StringBuilder content = new StringBuilder("<==今日高抛低吸策略==>\n");
        BigDecimal riseBit = new BigDecimal(3); //上涨三个点
        List<FundStockLast> riseStocks = fundStockLastRepository.findFundStockLastByPcGreaterThanEqualAndStatistics(riseBit, now);  //统计今天大涨的股票
        if (!CollectionUtils.isEmpty(riseStocks)) {
            riseStocks.sort(((o1, o2) -> o2.getPc().compareTo(o1.getPc()))); //按照持仓数量多少排序
            content.append("上涨超过三个点的股票一共" + riseStocks.size() + "支: \n");
            for (int i = 0; i < riseStocks.size(); i++) {
                FundStockLast fundStockLast = riseStocks.get(i);
                content.append((i + 1) + ":" + fundStockLast.get名字() + "(" + fundStockLast.getPc() + "%)").append(fundStockLast.getP()).append("\n");
                System.out.println(content.toString() + "===========");
            }
        }

        content.append("\n");
        BigDecimal fallBit = new BigDecimal(-3); //下跌三个点
        List<FundStockLast> fallStocks = fundStockLastRepository.findFundStockLastByPcLessThanEqualAndStatistics(fallBit, now); //统计今天大跌的股票
        if (!CollectionUtils.isEmpty(fallStocks)) {
            fallStocks.sort(((o1, o2) -> o2.getPc().compareTo(o1.getPc()))); //按照持仓数量多少排序
            content.append("下跌超过三个点的股票一共" + fallStocks.size() + "支: \n");
            for (int i = 0; i < fallStocks.size(); i++) {
                FundStockLast fundStockLast = fallStocks.get(i);
                content.append((i + 1) + ":" + fundStockLast.get名字() + "(" + fundStockLast.getPc() + "%)").append(fundStockLast.getP()).append("\n");
            }
        }

        SendGroupMessageReq sendGroupMessageReq2 = new SendGroupMessageReq();
        sendGroupMessageReq2.setCode("moreMoney");
        sendGroupMessageReq2.setContent(content.toString());
        sendGroupMessageReq2.setAtMobiles(dingDingConfig.getAtMobiles());
        dingDingGroupMessageService.sendGroupMessage(sendGroupMessageReq2);

        //相比上一个交易新增和减少股票提醒

    }
}
